package nancal.mp.mvc.dev

import io.swagger.annotations.*
import nancal.mp.db.mongo.*
import nancal.mp.db.mongo.entity.dev.*
import nancal.mp.mvc.dev.ApiDefineAutoController.CnTypeEnum.*
import nbcp.base.annotation.*
import nbcp.base.annotation.mvc.*
import nbcp.base.comm.*
import nbcp.base.extend.*
import nbcp.mvc.sys.*
import nbcp.myoql.db.mongo.*
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.*
import javax.servlet.http.*

/**
 * Created by CodeGenerator at 2023-10-16 23:06:13
 */
@Api(description = "Api定义", tags = arrayOf("ApiDefine"))
@RestController
@RequestMapping("/dev/api-define-profile")
class ApiDefineProfileAutoController {
    @PostMapping("/save-as")
    fun saveAs(
        ids: Array<String>,
        name: String
    ): JsonResult {
        mor.dev.apiDefineProfile.delete()
            .where { it.name mongoEquals name }
            .exec()

        mor.dev.apiDefineProfile.batchInsert()
            .apply {
                ids.forEach {
                    var ent = ApiDefineProfile();
                    ent.name = name;
                    ent.apiId = it;
                    this.add(ent);
                }
            }
            .exec();

        return JsonResult();
    }


    @PostMapping("/profiles")
    fun profiles(): List<String> {
        return mor.dev.apiDefineProfile.aggregate()
            .rawGroup {
                JsonMap(
                    "_id" to "$" + it.name
                )
            }
            .toMapList()
            .map { it.get("id").AsString() }
    }

    @Autowired
    lateinit var apiDefineAutoController: ApiDefineAutoController;

    @PostMapping("/load")
    fun load(name: String): ListResult<ApiDefine> {
        var ids = mor.dev.apiDefineProfile.query()
            .where { it.name mongoEquals name }
            .toList()
            .map { it.apiId }
            .toTypedArray()

        return apiDefineAutoController.list(
            "",
            "",
            "",
            "",
            null,
            ids,
            PagerModel()
        )
    }
}